/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.deployment.command;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.uml2.uml.AggregationKind;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.LiteralInteger;
import org.eclipse.uml2.uml.LiteralUnlimitedNatural;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.diagram.classdiagram.helper.ClassDiagramModelerHelper;
import com.cea.papyrus.diagram.deployment.helper.DeploymentDiagramModelerHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * 
 */
public class AssociationCreateCommand extends com.cea.papyrus.core.commands.association.AssociationCreateCommand {

	/**
	 * 
	 * 
	 * @param type 
	 * @param location 
	 * @param source 
	 */
	public AssociationCreateCommand(GraphElement source, Object type, Point location) {
		super(source, type, location);
	}

	/**
	 * Create the bussiness model.
	 * The created model should be stored by the implementation class for futur use (in reconnect/disconnect)
	 * 
	 * @return The created object.
	 */
	@Override
	public Element createBussinessModel() {
		// !! warning : the edge is still not created
		//connect the graphEdge to the diagram and UML Elements
		final Classifier umlSource = (Classifier)getSourceElement();
		final Classifier umlTarget = (Classifier)getTargetElement();

		association = (Association)ModelerHelper.getFactory().createUMLElement(Association.class, getUMLParent());
		
		// create 1st property
		targetProperty = (Property)ClassDiagramModelerHelper.getInstance().createUMLElement(Property.class, umlTarget.getName().toLowerCase(), association);
		targetProperty.setAggregation(AggregationKind.NONE_LITERAL);
		targetProperty.setType(umlTarget);
		LiteralInteger theint= UMLFactory.eINSTANCE.createLiteralInteger();
		theint.setValue(1);
		targetProperty.setLowerValue(theint);
		LiteralUnlimitedNatural thenatural=UMLFactory.eINSTANCE.createLiteralUnlimitedNatural();
		thenatural.setValue(1);
		targetProperty.setUpperValue(thenatural);
		
		// create 2nd property
		sourceProperty = (Property)ClassDiagramModelerHelper.getInstance().createUMLElement(Property.class, umlSource.getName().toLowerCase(), association);
		sourceProperty.setType(umlSource);
		sourceProperty.setAggregation(AggregationKind.NONE_LITERAL);
		thenatural =UMLFactory.eINSTANCE.createLiteralUnlimitedNatural();
		thenatural.setValue(1);
		sourceProperty.setUpperValue(thenatural);
		theint= UMLFactory.eINSTANCE.createLiteralInteger();
		theint.setValue(1);
		sourceProperty.setLowerValue(theint);
		
		//connect UML element
		ModelerHelper.getFactory().connect(Association.class, sourceProperty, targetProperty, association);
		
		// Return the created bussiness element. It is stored inside the GraphEdge.
		return association;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.commands.ConnectionCreateCommand#disconnectBussinessModel(com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge, com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge, com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge)
	 */
	/**
	 * 
	 * 
	 * @param bridge 
	 * @param targetBridge 
	 * @param sourceBridge 
	 */
	@Override
	public void disconnectBussinessModel(Uml1SemanticModelBridge bridge, Uml1SemanticModelBridge sourceBridge, Uml1SemanticModelBridge targetBridge) {
		// deconnect association from its end properties
		ModelerHelper.getFactory().deconnect(Association.class, sourceProperty, targetProperty, association);
		
		//remove properties
		DeploymentDiagramModelerHelper.getInstance().removeElement(Property.class, sourceProperty, sourceProperty.getOwner());
		DeploymentDiagramModelerHelper.getInstance().removeElement(Property.class, targetProperty, targetProperty.getOwner());
		
		// remove association
		ModelerHelper.getFactory().removeElement(Association.class, association, getUMLParent());
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.commands.ConnectionCreateCommand#reconnectBussinessModel(com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge, com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge, com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge)
	 */
	/**
	 * 
	 * 
	 * @param bridge 
	 * @param targetBridge 
	 * @param sourceBridge 
	 */
	@Override
	public void reconnectBussinessModel(Uml1SemanticModelBridge bridge, Uml1SemanticModelBridge sourceBridge, Uml1SemanticModelBridge targetBridge) {
		// add association
		ModelerHelper.getFactory().addUMLElement(Association.class, association, getUMLParent());
		
		DeploymentDiagramModelerHelper.getInstance().addUMLElement(Property.class, targetProperty, association);
		DeploymentDiagramModelerHelper.getInstance().addUMLElement(Property.class, sourceProperty, association);
		
		// connect association
		ModelerHelper.getFactory().connect(Association.class, sourceProperty, targetProperty, association);
	}

}
